package jinyilw.swing.visuals;

import jinyilw.common.Operator;
import jinyilw.swing.Direction;
import jinyilw.swing.FontTools;
import jinyilw.swing.WindowTools;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.theme.RColorSet;
import jinyilw.swing.visuals.Rcomp.RButton;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

public class TipWindow extends ImageWindow // 提示窗
{
	private static final long serialVersionUID = -8449534238834001142L;
	String[] tip;
	int tipCount = 0;
	Operator<?> finishOperator;
	Window owner;
	Point location;
	JLabel tipLabel = new JLabel();
	JLabel iLabel = new JLabel();
	JLabel closeLabel = new JLabel();
	RButton okButton = new RButton();
	RButton nButton = new RButton();

	public TipWindow(String[] str, Window window)
	{
		super(window);
		tip = str;
		owner = window;
		location = owner.getLocation();
		owner.addComponentListener(new LocationListener());
		rounded = false;
		setSize(240, 95);
		WindowTools.setWindowOpacity(window, 0.7f);
		setShape(new RoundRectangle2D.Double(0, 0, 240, 95, 120, 90));
		setAlwaysOnTop(true);
		Container content = getContentPane();
		content.setLayout(null);
		tipLabel.setFont(FontTools.getCustomFont(14));
		tipLabel.setHorizontalAlignment(SwingConstants.CENTER);
		tipLabel.setForeground(Color.RED);
		tipLabel.setBounds(3, 30, 235, 26);
		tipLabel.setText(tip[0]);
		iLabel.setBounds(85, 7, 65, 17);
		iLabel.setText("Tip " + 1 + "/" + tip.length);
		iLabel.setForeground(Color.black);
		okButton.setForeground(new Color(0, 128, 0));
		okButton.setFont(FontTools.getCustomFont(13));
		okButton.setBackground(Color.WHITE);
		okButton.addActionListener(new OKButtonActionListener());
		okButton.setText("确 定");
		okButton.setBounds(50, 61, 60, 22);
		okButton.setDefaultAlpha(0.55f);
		nButton.setText("不再显示");
		nButton.setForeground(new Color(0, 128, 0));
		nButton.setFont(FontTools.defaultFont);
		nButton.setBackground(Color.WHITE);
		nButton.setBounds(115, 61, 78, 22);
		nButton.setDefaultAlpha(0.55f);
		nButton.addActionListener(e -> {
			dispose();
			if (finishOperator != null)
				finishOperator.operate(null);
		});
		closeLabel.addMouseListener(new CloseLabelMouseListener());
		closeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		closeLabel.setFont(FontTools.getCustomFont(18));
		closeLabel.setForeground(Color.black);
		closeLabel.setText("×");
		closeLabel.setBounds(150, 7, 17, 17);
		closeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		content.add(tipLabel);
		content.add(okButton);
		content.add(nButton);
		content.add(iLabel);
		content.add(closeLabel);

		setOpacity(130);
		setBackground(RColorSet.lightBGColor);
	}

	public void setFinishOperator(Operator<?> operator)
	{
		finishOperator = operator;
	}

	public void showInitTipWindow()
	{
		WindowTools.showTipWindow(this, owner, Direction.DOWN);
	}

	public void showTipWindow()
	{
		WindowTools.showTipWindow(this, this, Direction.DOWN);
	}

	@Override
	public void paint(Graphics g)
	{
		setBGImage(UITools.getSmallBlurLightImage());
		super.paint(g);
	}

	private class OKButtonActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			tipCount++;
			if (tipCount < tip.length)
			{
				tipLabel.setText(tip[tipCount]);
				iLabel.setText("Tip " + (tipCount + 1) + "/" + tip.length);
				showTipWindow();
			} else
			{
				tipCount = 0;
				tipLabel.setText(tip[tipCount]);
				iLabel.setText("Tip " + (tipCount + 1) + "/" + tip.length);
				dispose();
				if (finishOperator != null)
					finishOperator.run();
			}
		}
	}

	private class CloseLabelMouseListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(final MouseEvent e)
		{
			dispose();
		}
	}

	private class LocationListener extends ComponentAdapter
	{
		@Override
		public void componentMoved(final ComponentEvent e)
		{
			Point newLocation = owner.getLocation();
			int x = newLocation.x - location.x;
			int y = newLocation.y - location.y;
			TipWindow.this.setLocation(TipWindow.this.getLocation().x + x,
					TipWindow.this.getLocation().y + y);
			location = newLocation;
		}
	}
}
